﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClickerClimber.Scenes.SplashScreenScene;
using ClickerClimber.Scenes;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;

namespace ClickerClimber.Managers
{
    class AudioManager
    {
        private static AudioManager _instance;
        
        private ContentManager _content;

        SoundEffectInstance currentMusic;
        // Music 
        SoundEffect titleMusic;
        SoundEffect gameplayMusic;
        SoundEffect gameWin;
        SoundEffect gameOver;

        SoundEffect clearSmall;
        SoundEffect clearMedium;
        SoundEffect clearLarge;
        SoundEffect invalidClick;
        SoundEffect blockCreated;
        SoundEffect blockDamaged;
        SoundEffect blockDestroyed;
        SoundEffect enemyKilled;
        SoundEffect enemyAppearing;
        SoundEffect climbing;
        SoundEffect falling;
        SoundEffect timerEnding;

        //Set the sound effects to use
        SoundEffect soundWoow;

        private int _previousHeight = 0;

        public enum eSound
        {
            eSound_Wow = 0,
            titleMusic,
            gameplayMusic,
            gameWin,
            gameOver,
            clearSmall,
            clearMedium,
            clearLarge,
            invalidClick,
            blockCreated,
            blockDamaged,
            blockDestroyed,
            enemyKilled,
            enemyAppearing,
            climbing,
            falling,
            timerEnding
        }

        #region Constructors

        private AudioManager()
        {
        }

        /// <summary>
        /// Gets the singleton instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static AudioManager Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new AudioManager();
                }
                return _instance;
            }
        }

        /// <summary>
        /// Starts up the Singleton
        /// </summary>
        public void StartUp()
        {
            LoadSounds();
        }

        /// <summary>
        /// LoadSounds
        /// </summary>
        public void LoadSounds()
        {
            soundWoow = _content.Load<SoundEffect>(GameParameters.SOUND_WOOW_NAME);
            titleMusic = _content.Load<SoundEffect>(GameParameters.MUSIC_TITLE);
            gameplayMusic = _content.Load<SoundEffect>(GameParameters.MUSIC_GAME);
            gameWin = _content.Load<SoundEffect>(GameParameters.MUSIC_WIN);
            gameOver = _content.Load<SoundEffect>(GameParameters.MUSIC_LOSE);
            clearSmall = _content.Load<SoundEffect>(GameParameters.SOUND_CLEAR_SMALL);
            clearMedium = _content.Load<SoundEffect>(GameParameters.SOUND_CLEAR_MEDIUM);
            clearLarge = _content.Load<SoundEffect>(GameParameters.SOUND_CLEAR_LARGE);
            invalidClick = _content.Load<SoundEffect>(GameParameters.SOUND_INVALID_CLICK);
            blockCreated = _content.Load<SoundEffect>(GameParameters.SOUND_BLOCK_CREATED);
            blockDamaged = _content.Load<SoundEffect>(GameParameters.SOUND_BLOCK_DAMAGED);
            blockDestroyed = _content.Load<SoundEffect>(GameParameters.SOUND_BLOCK_DESTROYED);
            enemyKilled = _content.Load<SoundEffect>(GameParameters.SOUND_ENEMY_KILLED);
            enemyAppearing = _content.Load<SoundEffect>(GameParameters.SOUND_ENEMY_APPEARING);
            climbing = _content.Load<SoundEffect>(GameParameters.SOUND_CHARACTER_CLIMB);
            falling = _content.Load<SoundEffect>(GameParameters.SOUND_CHARACTER_FALL);
            timerEnding = _content.Load<SoundEffect>(GameParameters.SOUND_LAST_10_SECONDS);
        }

        public void UpdateMusicPitch(int _height)
        {
            if (_height != _previousHeight && (_height - _previousHeight) % 10 == 0)
            {
                _previousHeight = _height;
                if (currentMusic.Pitch < GameParameters.MAX_MUSIC_PITCH)
                {
                    currentMusic.Pitch = currentMusic.Pitch + 0.0625f; 
                }                
            }
        }

        public void playBackgroundMusic(eSound _music){
            this.StopMusic();
            switch (_music)
            {
                case eSound.gameplayMusic:                    
                    currentMusic = gameplayMusic.CreateInstance();
                    currentMusic.Volume = 0.15f;
                    currentMusic.Pitch = GameParameters.MIN_MUSIC_PITCH;
                    _previousHeight = 5;
                    break;
                case eSound.titleMusic:
                    currentMusic = titleMusic.CreateInstance();
                    break;
            }
            if (currentMusic != null)
            {
                currentMusic.IsLooped = true;
                currentMusic.Play();
            }

        }

        public void StopMusic()
        {
            if (currentMusic != null)
            {
                currentMusic.Stop();
                currentMusic.Dispose();
                currentMusic = null;
            }
        }
        /// <summary>
        /// PlaySound
        /// </summary>
        /// <param name="_sound"></param>
        public void PlaySound(eSound _sound)
        {
            switch (_sound)
            {
                case eSound.eSound_Wow:
                    soundWoow.Play();
                    break;
                case eSound.blockCreated:
                    blockCreated.Play();
                    break;
                case eSound.blockDamaged:
                    blockDamaged.Play();
                    break;
                case eSound.blockDestroyed:
                    blockDestroyed.Play();
                    break;
                case eSound.clearLarge:
                    clearLarge.Play();
                    break;
                case eSound.clearMedium:
                    clearMedium.Play();
                    break;
                case eSound.clearSmall:
                    clearSmall.Play();
                    break;
                case eSound.climbing:
                    climbing.Play();
                    break;
                case eSound.enemyAppearing:
                    enemyAppearing.Play();
                    break;
                case eSound.enemyKilled:
                    enemyKilled.Play();
                    break;
                case eSound.falling:
                    falling.Play();
                    break;
                case eSound.gameOver:
                    gameOver.Play();
                    break;
                case eSound.gameWin:
                    gameWin.Play();
                    break;
                case eSound.invalidClick:
                    invalidClick.Play();
                    break;
                case eSound.timerEnding:
                    timerEnding.Play();
                    break;
            }
        }


        #endregion Constructors

        #region Properties
        public ContentManager Content
        {
            set
            {
                _content = value;
            }
        }

        #endregion Properties
    }
}
